package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.SpuSubjectMappingMapper;
import mobi.hami.huapin.dao.SubjectMapper;
import mobi.hami.huapin.model.Product;
import mobi.hami.huapin.service.ManagementSubjectService;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import mobi.hami.huapin.model.Subject;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 管理后台-主题-相关业务
 */
@Service
public class ManagementSubjectServiceImpl implements ManagementSubjectService {

    private static final Logger logger = LoggerFactory.getLogger(ManagementSubjectService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SpuSubjectMappingMapper spuSubjectMappingMapper;

    /**
     * 多条件查询主题
     * @param parms
     * @return
     */
    public List<Subject> selectUserInvoiceWitchParam(Map<String,Object> parms){
        return subjectMapper.selectSubjectsWitchParam(parms);
    }
    /**
     * 删除主题
     */
    public Result delete(Integer id){
        if(null == id){
            return Results.failureWithStatus(1,"主题id为空");
        }
        Subject sb = subjectMapper.selectById(id);
        if(null == sb){
            return Results.failureWithStatus(1,"该主题不存在");
        }
        if(sb.getDeleted() == 1){
            return Results.failureWithStatus(1,"该主题已经被删除过,不能再删除");
        }
        /**
         * //删除状态，0表示正常，1表示已经删除
         */
        if(subjectMapper.updateDeletedById(id,1,new Date()) > 0 ){
            return Results.successWithStatus(0,"操作成功!");
        }
        return Results.failureWithStatus(1,"删除失败");
    }

    /**
     * 创建主题
     */
    public Result addSubject(Map<String,Object> mm){
        if(null == mm || mm.isEmpty()){
            return Results.failureWithStatus(1,"参数为空");
        }
        if(null == mm.get("name")){
            return Results.failureWithStatus(1,"主题名称为空");
        }
        if(null == mm.get("day_of_week")){
            return Results.failureWithStatus(1,"具体周几数,例如1则为周一 的参数为空");
        }
        if(null == mm.get("description")){
            return Results.failureWithStatus(1,"主题描述为空");
        }
        String name = String.valueOf(mm.get("name"));
        Integer day_of_week = new Integer(String.valueOf(mm.get("day_of_week")));
        String description = String.valueOf(mm.get("description"));
        Subject sj = subjectMapper.findOneByName(name);
        if(null == sj){
            Subject sb = new Subject();
            sb.setName(name);
            sb.setDay_of_week(day_of_week);
            sb.setDescription(description);
            sb.setDeleted(0);
            sb.setUse_status(1);
            sb.setGmt_create(new Date());
            sb.setGmt_modified(new Date());
            if(subjectMapper.insertSelective(sb) > 0){
                return Results.successWithStatus(0,"操作成功!");
            }
        }
        if(null != sj){
            if(sj.getUse_status() == 0){//状态：0表示未启用，1表示启用
                sj.setUse_status(1);
                sj.setGmt_modified(new Date());
                sj.setGmt_create(new Date());
            }
            if(sj.getUse_status() == 0){
                return Results.failureWithStatus(1,"该主题名称已经存在!");
            }
        }
        return Results.failureWithStatus(1,"操作失败!");
    }

    /**
     * 修改主題
     * @param sb
     * @return
     */
    public Result updateSubject(Subject sb){
        if(null == sb){
            return Results.failureWithStatus(1,"参数为空!");
        }
        if(null == sb.getId()){
            return Results.failureWithStatus(1,"参数id为空!");
        }
        sb.setGmt_modified(new Date());
        subjectMapper.updateSb(sb);
        if( subjectMapper.updateByPrimaryKey(sb) > 0){
            return Results.successWithStatus(0,"操作成功!");
        }
        return Results.failureWithStatus(1,"操作失败!");
    }
    /**
     * 主题下的商品列表
     * @param mm
     * @return
     */
    public List<Product> subject_products(Map<String,Object> mm){
        return spuSubjectMappingMapper.subject_products(mm);
    }
    /**
     * 主题管理-把某个商品从某个主题下删除/上綫
     * @param pid 商品id
     * @param subid 主题id
     ** @param  deleted 删除状态，0表示正常，1表示已经删除
     * @return
     */
    public Result updatesmdelete(Integer pid,Integer subid,Integer deleted){
        if(null == pid || pid.equals("")){
            return Results.failureWithStatus(1,"商品id为空!");
        }
        if(null == subid || subid.equals("")){
            return Results.failureWithStatus(1,"主题id为空!");
        }
        if(null == deleted || deleted.equals("")){
            return Results.failureWithStatus(1,"deleted为空!");
        }
        if(spuSubjectMappingMapper.updatesmdelete(pid,subid,deleted) > 0){
            return Results.successWithStatus(0,"操作成功!");
        }
        return Results.failureWithStatus(1,"操作失败!");
    }

}
